﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Resources;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Windows.Media.Imaging;

namespace Sanctuary.Core
{
    public class Characters
    {
        public const string FILE_NAME = "CharacterDefinition.xml";
        private static StreamResourceInfo zipInfo;

        public Characters(XDocument document, StreamResourceInfo zip)
        {
            zipInfo = zip;

            foreach (XElement item in document.Descendants("Character"))
            {
                Character c = new Character()
                {
                    Name = item.Attribute("Name").Value,
                    DisplayName = item.Attribute("DisplayName").Value
                };
                foreach (XElement emotion in item.Descendants("Emotion"))
                {
                    c.AddEmotion(new CharacterEmotion()
                    {
                        Name = emotion.Attribute("Name").Value,
                        Path = emotion.Value
                    });
                }
                characterItems.Add(c.Name, c);
            }
        }

        private Dictionary<string, Character> characterItems = new Dictionary<string, Character>();

        public Character this[string characterName]
        {
            get
            {
                if (!characterItems.ContainsKey(characterName))
                {
                    throw new GameContentException("Character: " + characterName + " not conatined in " + FILE_NAME);
                }
                return characterItems[characterName];
            }
        }

        public void LoadEmotion(Image image, string characterName, string emotionName)
        {
            Character c = this[characterName];
            StreamResourceInfo sr = Application.GetResourceStream(zipInfo, new Uri(c[emotionName].Path, UriKind.Relative));
            BitmapImage bmp = new BitmapImage();
            bmp.SetSource(sr.Stream);
            image.Source = bmp;
        }
    }
}
